package com.flame.workflow.adapter;

import com.flame.user.controller.vo.RoleVo;
import com.flame.user.entity.OrgInfo;
import com.flame.user.entity.Role;
import com.flame.user.entity.UserInfo;
import com.flame.user.mapper.OrgInfoMapper;
import com.flame.user.mapper.RoleMapper;
import com.flame.user.mapper.UserInfoMapper;
import com.flame.user.mapper.UserRoleMapper;
import com.flame.workflow.id.entity.WorkflowOrganization;
import com.flame.workflow.id.entity.WorkflowRole;
import com.flame.workflow.id.entity.WorkflowUser;
import com.flame.workflow.id.service.IdentityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author wuxintong😺😺😺
 * @since 2023/11/6
 */
@Service
public class FlameIdentityServiceImpl implements IdentityService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private OrgInfoMapper orgInfoMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<WorkflowUser> listWorkflowUserByUserIds(List<String> userIdList) {
        List<WorkflowUser> res = new ArrayList<>();
        for (String userId : userIdList) {
            UserInfo userInfo = userInfoMapper.findById(userId);
            WorkflowUser workflowUser = new WorkflowUser();
            BeanUtils.copyProperties(userInfo, workflowUser);
            res.add(workflowUser);
        }
        return res;
    }

    @Override
    public List<WorkflowRole> listWorkflowRole() {
        RoleVo roleVo = new RoleVo();
        roleVo.setPageNum(0);
        roleVo.setPageSize(0);
        List<Role> roleList = roleMapper.page(roleVo);
        List<WorkflowRole> res = new ArrayList<>();
        for (Role role : roleList) {
            WorkflowRole workflowRole = new WorkflowRole();
            BeanUtils.copyProperties(role, workflowRole);
            res.add(workflowRole);
        }
        return res;
    }

    @Override
    public List<WorkflowRole> listWorkflowRoleByRoleCodes(List<String> roleCodeList) {
        List<WorkflowRole> res = new ArrayList<>();
        for (String roleCode : roleCodeList) {
            Role role = roleMapper.selectById(roleCode);
            WorkflowRole workflowRole = new WorkflowRole();
            BeanUtils.copyProperties(role, workflowRole);
            res.add(workflowRole);
        }
        return res;
    }

    @Override
    public List<WorkflowOrganization> listWorkflowOrganization() {
        return orgInfoMapper.list().stream().map(orgInfo -> {
            WorkflowOrganization organization = new WorkflowOrganization();
            BeanUtils.copyProperties(orgInfo, organization);
            return organization;
        }).collect(Collectors.toList());
    }

    @Override
    public WorkflowUser getUserByUserId(String userId) {
        UserInfo userInfo = userInfoMapper.findById(userId);
        WorkflowUser workflowUser = new WorkflowUser();
        BeanUtils.copyProperties(userInfo, workflowUser);
        return workflowUser;
    }

    @Override
    public WorkflowOrganization getOrgByOrgCode(String orgCode) {
        OrgInfo orgInfo = orgInfoMapper.findById(orgCode);
        WorkflowOrganization organization = new WorkflowOrganization();
        BeanUtils.copyProperties(orgInfo, organization);
        return organization;
    }

    @Override
    public Set<String> listWorkflowUserIdByRoleCode(String roleCode) {
        return userRoleMapper.listUserByRoleCode(roleCode);
    }

    @Override
    public List<WorkflowUser> listWorkflowUserByOrgCode(String orgCode) {
        return userInfoMapper.listByOrgCode(orgCode).stream().map(userInfo -> {
            WorkflowUser workflowUser = new WorkflowUser();
            BeanUtils.copyProperties(userInfo, workflowUser);
            return workflowUser;
        }).collect(Collectors.toList());
    }
}
